<?php
/*
 * @description: Модель для работы с историей
 * @copyright Copyright (c) 2012-2017 Marin V. , Tykva A. , Kozel R. , Sokolov D.
 * @version 1.0
 * @link http://lecturebox.ru
*/

class Model_Subscribe
{
    protected $lecture_id;

    protected $_dbTable;
    protected $_row;
    
    protected $data;
    
    public function __construct($lecture_id=0)
    {
        $this->lecture_id = $lecture_id;
        $this->data = null;

        $this->_dbTable = new Model_DbTable_Subscribes();
        if($lecture_id)
            $this->_row = $this->_dbTable->find($lecture_id)->current();
        else
            $this->_row = $this->_dbTable->createRow();
    }
    
    public function setSubscribe($cid, $uid)
    {   
        $data = array(
            'course_id'      => $cid,
            'user_id'      => $uid   
        ); 
        $this->_dbTable->insert($data);
    }
    
    public function delSubscribe($cid, $uid)
    {   
        $where = array(
            'course_id = ?' => $cid,
            'user_id = ?' => $uid
        );
        $this->_dbTable->delete($where);
    }
    
    public function getSubscribes($cid)
    {   
        $db = Zend_Db_Table::getDefaultAdapter();
        $select = $db->select()
                ->from(array('d' => 'lecbox_subscribes'),
                       array('course_id', 'user_id'))
                ->where('d.course_id = ?', $cid)
                ->join(array('c' => 'lecbox_users'),
                       'd.user_id = c.id',
                       array('email') );               

        return $db->fetchAll($select);
    }
    
    public function checkSubscribe($cid, $uid)
    {   
        $select = $this->_dbTable->select();
        $select->where('course_id = ?', $cid)
                ->where('user_id =?', $uid);
        return $this->_row = $this->_dbTable->fetchAll($select);
    }
    
    public function __set($name, $val)
    {
        if (isset($this->_row->$name)) {
            $this->_row->$name = $val;
        }
    }

    public function __get($name)
    {
        if (isset($this->_row->$name)) {
            return $this->_row->$name;
        }
    }
 
}
